# 3.8 RFID Sensor ## 3.8.1 Overview RFID-RC522 module adopts Phillips MFRC522 original chip in card reading circuit, which is easy to use and with low cost. It is suitable for equipment and reader development, advanced applications, RF card terminal design and producing. ## 3.8.2 Schematic Diagram ![6-8-2](./media/6-8-2.png) **RFID (Radio Frequency Identification)**: The card reader is composed of a frequency transmitter module and a high level magnetic field. The Tag transponder is a device to be sensed without a battery. It consists only of tiny integrated circuit chips, media for storing data, and antennas for receiving and transmitting signals. To read the data in the tag, it must be placed within the reading range of the reader. After that, the reader will generate a magnetic field. According to Lenz's law (magnetic energy generates electricity), the RFID Tag will be powered, thus activating the device.

NOTE: this module only recognize card working at 13.56MHz. It is recommended to use the provided card in the kit.

## 3.8.3 Code Blocks 1. ![j27](./media/j27.png) This block initializes RFID module. Without it, its data cannot be read. 2. ![28](./media/j28.png) This block reads the card value of the RFID sensor. 3. ![image-20240701104605293](./media/j29.png) This is a comparison block that determines whether the value of two parts equals each other. When they are equal, it outputs 1, or else it outputs 0. 4. ![image-20240701104539284](./media/j30.png) This is a logical block to reverse the value. That means this block outputs 0 when the condition block outputs 1, and it outputs 1 when the condition outputs 0. It reverses outputs. 5. ![image-20240701104509861](./media/j31.png) This is a conversion block that converts values into one of types of integer, decimal or string. ![image-20240701105332920](./media/j32.png) ## 3.8.4 Test Code ## 3.8.4.1 Build Code There are two ways to upload the code: directly open the code file we provide; or manually build blocks. **Directly open the code file we provide:** 1. Click ![](./media/j68.png) and choose `Load from your computer` ![](./media/j67.png) 2. We have already downloaded the codes on computer desktop, so open the file and choose `3-8-rfid.sb3` **Manually build blocks:** 1. In ![events](./media/events.png), find ![j1](./media/j1.png) block. 2. In ![rfid](./media/RFID.png), find and put ![j27](./media/j27.png) under ![j1](./media/j1.png) 3. In ![serial](./media/serial.png), find ![j12](./media/j12.png) and set baud rate to 9600 4. In ![control](./media/variable.png), put ![j7](./media/j7.png) under ![6-4-4-1-1](./media/6-2-4-1-1.png), and set variable Type to `string` 5. In ![control](./media/control.png), put ![j2](./media/j2.png) below the block ![](./media/j7.png) ![image-20240701131338008](./media/6-8-4-1-2.png) 6. In ![control](./media/variable.png), place ![j9](./media/j9.png) in ![j2](./media/j2.png) 7. In ![rfid](./media/rfid.png), put ![j21](./media/j28.png) in the right box of block ![j9](./media/j9.png) 8. In ![control](./media/control.png), add a ![j25](./media/j25.png) under ![image-20240701131625149](./media/6-8-4-1-1.png) 9. In ![operator](./media/operator.png), put ![j22](./media/j30.png) into the condition box of ![j25](./media/j25.png) 10. In ![operator](./media/operator.png), put ![j22](./media/j29.png) into the condition box of ![j25](./media/j30.png) ![image-20240701131910235](./media/6-8-4-1-3.png) 11. In ![control](./media/variable.png), put ![j9](./media/j8.png) into the left part of ![j22](./media/j29.png) 12. In ![image-20240701132101678](./media/data.png), put ![j9](./media/j31.png) into the right part of ![j22](./media/j29.png) and modify the conversion value to “0” and conversion type to "string". ![image-20240701132257387](./media/6-8-4-1-4.png) 13. In ![serial](./media/serial.png), find ![j13](./media/j13.png) and put it into ![image-20240701132431720](./media/6-8-4-1-5.png), and modify the printing content into “RFID:” and set printing mode to no-warp. 14. Drag another ![j13](./media/j13.png), and then add a variable block ![j9](./media/j8.png) into the printing content box. As follows: ![image-20240701132734670](./media/6-8-4-1-6.png) **Complete Test Code** ![image-20240701132810067](./media/6-8-4-1-7.png) ## 3.8.4.2 Test Result After uploading code, cover the RFID sensing area with the IC card or the key in the kit, and you will see the serial monitor prints the ID numbers. ![image-20240701133030371](./media/6-8-4-2.png)